package com.ruan.exception;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

//自定义日期数据的反序列化过程，确保正确地将日期字符串转换为java.sql.Date对象，
// 从而避免IllegalArgumentException
public class CustomDateDeserializer extends JsonDeserializer<Date> {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        String dateString = jsonParser.getText();
        try {
            // 解析日期字符串并创建java.sql.Date对象
            java.util.Date date = sdf.parse(dateString);
            return new Date(date.getTime());
        } catch (ParseException e) {
            throw new IOException("Error parsing date: " + dateString, e);
        }
    }
}
